#include <LiquidCrystal.h>
const int rs = 13, en = 12, d4 = 3, d5 = 2, d6 = 1, d7 = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int numRows = 4;//定义 4 行
const int numCols = 4;//定义 4 列
const int debounceTime = 20;//去抖动时间长度
const char keymap[numRows][numCols]= {//键值，可以按需要更改
 { 'D','#','0','*' }, 
 {'C','9','8','7' }, 
 {'B','6','5','4' }, 
 {'A','3','2','1' } 
}; 
const int rowPins[numRows] = {7,6,5,4};//设置硬件对应的引脚
const int colPins[numCols] = {11,10,9,8};
//初始化功能
void setup(){ 
Serial.begin(9600); 
lcd.begin(16, 2);
for(int row = 0; row < numRows; row++){ 
pinMode(rowPins[row],INPUT); 
digitalWrite(rowPins[row],HIGH); 
} 
for(int column = 0;column < numCols; column++){ 
 pinMode(colPins[column],OUTPUT); 
 digitalWrite(colPins[column],HIGH); 
} 
} 
//主循环
void loop() { 
 // 添加其他的程序，循环运行
 char key = getkey(); 
 if(key !=0){
  Serial.print("Got key ");//串口打印键值
 Serial.println(key); 
 int key1 = key - 48;
 Serial.println(key1);
 lcd.print(1234);
 delay(100);
 } 
 
 } 
//读取键值程序
char getkey(){ 
 char key = 0; 
 for(int column = 0;column < numCols; column++){ 
 digitalWrite(colPins[column],LOW); 
 for(int row = 0 ;row < numRows; row++){ 
 if(digitalRead(rowPins[row]) == LOW){ //是否有按键按下
 delay(debounceTime); 
 while(digitalRead(rowPins[row]) == LOW) //等待按键释放
 ; 
 key = keymap[row][column]; 
 } 
 } 
 digitalWrite(colPins[column],HIGH); //De-active the current column 
 } 
 return key; 
 }
 
